package bingliandshuju;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import zhzz.SingleDataofzhzz;
import bingliandshuju.BingLi3.listenerofBL3F;

import com.example.animaldoctor03.AllIllness;
import com.example.animaldoctor03.JBXQ;
import com.example.animaldoctor03.R;
import com.example.animaldoctor03.YiXuanZhengZhuang;
import com.example.animaldoctor03.ZhengDuanFF;
import com.example.animaldoctor03.ZhengZhuangFZ;

import commonClass.ADAQueryClinicalSymptomsKeyByDiseaseIDAction;
import commonClass.FunctionDispatcher;
import commonClass.RemoteActionsName;
import commonClass.FunctionDispatcher.IRequestCompeletListener;
import Modle.disease.ClinicalSymptomsKey;
import Modle.disease.Symptoms;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification.Builder;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class BingLi4 extends Activity {

	private String nameofBL4 = "";
	private String GLofBL4;
	private TextView textofBL4 = null;
	private ListView listviewofbingli4 = null;
	private Button buttonofbingli4toJBXQ = null,
			buttonofbingli4forallchoice = null;
	private Button buttonofbingli4toagain = null,buttonofBL4F = null;
	private Symptoms symptomsofBL4getinformation = null;
	private List<Object> listofBL4getinformation = new ArrayList<Object>();
	private String animalIDofBL4 = "";
	private ArrayList<Object> resultListofBL4 = null;

	private void initElemofBL4() {
		setContentView(R.layout.bingli4);

		buttonofBL4F = (Button) findViewById(R.id.buttonofBLF4);
		textofBL4 = (TextView) findViewById(R.id.textviewofbingli4);
		listviewofbingli4 = (ListView) findViewById(R.id.listofbingli4);
		buttonofbingli4forallchoice = (Button) findViewById(R.id.buttonofbingli44);
		buttonofbingli4toJBXQ = (Button) findViewById(R.id.buttonofbingli41);
		buttonofbingli4toagain = (Button) findViewById(R.id.buttonofbingli42);

		buttonofbingli4toJBXQ.setOnClickListener(new listenetofbingli4toJBXQ());
		buttonofbingli4toagain
				.setOnClickListener(new listenerofbingli4toagain());
		buttonofBL4F.setOnClickListener(new listenerofBL4F());
	}

	private void getInformationofBL4() {

		SingleDataofAll singleDataofBL4 = SingleDataofAll.getInstance();
		symptomsofBL4getinformation = singleDataofBL4.getObject();
		listofBL4getinformation = singleDataofBL4.getListinformation();
	}

	private void getInformationfromupofBL4() {
		Intent intentofBL4 = getIntent();
		Bundle bundleofBL4 = intentofBL4.getExtras();
		ClinicalSymptomsKey CofBL4 = (ClinicalSymptomsKey) bundleofBL4
				.getSerializable("shuju");
		animalIDofBL4 = CofBL4.getDiseaseID();
		nameofBL4 = CofBL4.getDiseaseName();
		DecimalFormat formatofBL4 = new DecimalFormat("#.0000");
		GLofBL4 = formatofBL4.format(CofBL4.getDiseaseProbability());
		Message msgofBL4 = new Message();
		msgofBL4.what = 1;
		handleofBL4.sendMessage(msgofBL4);
	}

	private ArrayList<Object> assemblyofBL4() {

		getInformationfromupofBL4();
		ArrayList<Object> list = new ArrayList<Object>();

		list.add(animalIDofBL4);
		// Integer i = new Integer(10);
		// list.add(i);

		return list;
	}

	private void bindingofBL4(final ArrayList<Object> list) {
		try {
			getInformationofBL4();
			List<String> listofoldIdsofBL4 = new ArrayList<String>();
			listofoldIdsofBL4.add(symptomsofBL4getinformation.getSymptomsID());
			for (int i = 0; i < listofBL4getinformation.size(); i++) {

				ClinicalSymptomsKey ctextofBL4 = (ClinicalSymptomsKey) listofBL4getinformation
						.get(i);
				listofoldIdsofBL4.add(ctextofBL4.getClinicalSymptomsKeyID());

			}
			List<Object> listofZZhuanofBL4 = new ArrayList<Object>();

			List<Object> listofBL4QC = new ArrayList<Object>();

			for (int j = 0; j < list.size(); j++) {

				ClinicalSymptomsKey cetext1ofBL4 = (ClinicalSymptomsKey) list
						.get(j);
				String sofctext1ofBL4 = cetext1ofBL4.getClinicalSymptomsKeyID();
				for (String s : listofoldIdsofBL4) {
					if (sofctext1ofBL4.equals(s)) {

						listofBL4QC.add(cetext1ofBL4);
					}

				}
			}

			for (int i = 0; i < list.size(); i++) {

				int jishuofBL4 = 0;
				ClinicalSymptomsKey ctext2ofBL4 = (ClinicalSymptomsKey) list
						.get(i);
				String sofctext2ofBL4 = ctext2ofBL4.getClinicalSymptomsKeyID();
				for (String s : listofoldIdsofBL4) {
					if (!sofctext2ofBL4.equals(s)) {
						jishuofBL4++;

					}
					if (jishuofBL4 == listofoldIdsofBL4.size()) {
						listofZZhuanofBL4.add(ctext2ofBL4);
					}

				}
			}

			String[] resultofBL4 = new String[listofZZhuanofBL4.size()];
			// list.toArray(new String[list.size()]);
			for (int i = 0; i < listofZZhuanofBL4.size(); i++) {

				// haiyouwenti
				resultofBL4[i] = ((ClinicalSymptomsKey) listofZZhuanofBL4
						.get(i)).getClinicalSymptomsKey();
			}
			ArrayAdapter<String> arrayAdapterofBL4 = new ArrayAdapter<>(this,
					android.R.layout.simple_list_item_multiple_choice,
					resultofBL4);
			listviewofbingli4.setAdapter(arrayAdapterofBL4);
			buttonofbingli4forallchoice.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					ArrayList<Object> listofnextofBL4 = new ArrayList<Object>();

					for (long ids : listviewofbingli4.getCheckItemIds()) {

						int i = (int) ids;

						listofnextofBL4.add((ClinicalSymptomsKey) list.get(i));
						// System.out.println("#################" + ids);
					}

					// 把数据储存好
					SingleDataofAll singleDataofAllofBL4 = SingleDataofAll
							.getInstance();
					singleDataofAllofBL4.setListinformation(listofnextofBL4);
					try {
						Intent intentofbingli3toallchoice = new Intent(BingLi4.this,
								YiXuanZhengZhuang.class);
						startActivity(intentofbingli3toallchoice);
						finish();
					} catch (RuntimeException e) {
						android.app.AlertDialog.Builder builderofBL4oftoYXZZ = new AlertDialog.Builder(
								BingLi4.this);
						builderofBL4oftoYXZZ.setMessage("该功能不能使用,请重试或者使用其他功能");
						builderofBL4oftoYXZZ.create().show();
					}

				}
			});
		} catch (NullPointerException e) {
			android.app.AlertDialog.Builder builderofBL4 = new AlertDialog.Builder(
					BingLi4.this);
			builderofBL4.setMessage("无数据 , 请用其他功能");
			builderofBL4.create().show();
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		initElemofBL4();
		ArrayList<Object> parasListofBL4 = assemblyofBL4();

		final ArrayList<Object> resultListTofBL4 = null;

		ADAQueryClinicalSymptomsKeyByDiseaseIDAction iActionofBL4 = new ADAQueryClinicalSymptomsKeyByDiseaseIDAction();
		FunctionDispatcher functionDispacherofBL4 = new FunctionDispatcher();

		functionDispacherofBL4.exceute(
				RemoteActionsName.QueryClinicalSymptomsKeyByDiseaseIDAction,
				parasListofBL4, iActionofBL4, new IRequestCompeletListener() {

					@Override
					public void doCompelet(ArrayList<Object> resultList) {
						// TODO Auto-generated method stub
						try {
							Log.v("ZhengZhuangFZ2",
									"post:" + resultList.toString());
							BingLi4.this.resultListofBL4 = resultList;
							bindingofBL4(resultList);
						} catch (NullPointerException e) {
							android.app.AlertDialog.Builder builderofBL4off = new AlertDialog.Builder(
									BingLi4.this);
							builderofBL4off.setMessage("返回数据为空 ,请用其他功能");
							builderofBL4off.create().show();
						}
					}
				}, this);

	}

	class listenerofBL4F implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			try {
				SingleDataofAll sAll = SingleDataofAll.getInstance();
				sAll.removeAll();
				SingleInformationofIllnessDetail sInfor = SingleInformationofIllnessDetail
						.getInstance();
				sInfor.removeAll();
				SingleDataofzhzz sDofzh = SingleDataofzhzz.getInstance();
				sDofzh.removeAll();
				Intent intentofBL4F = new Intent(BingLi4.this,
						ZhengDuanFF.class);
				startActivity(intentofBL4F);
				finish();
			} catch (RuntimeException e) {
				android.app.AlertDialog.Builder builderofBL4ofF = new AlertDialog.Builder(
						BingLi4.this);
				builderofBL4ofF.setMessage("该功能不能使用,请重试或者使用其他功能");
				builderofBL4ofF.create().show();
			}

		}

	}

	class listenerofbingli4toagain implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub

			try {
				List<Object> listofagainofBL4 = new ArrayList<Object>();
				SingleDataofAll singDataofallagainofBL4 = SingleDataofAll
						.getInstance();
				listofagainofBL4 = singDataofallagainofBL4.getListinformation();
				Bundle bundleofagainofBL4 = new Bundle();
				bundleofagainofBL4.putSerializable("shuju",
						(Serializable) listofagainofBL4);
				Intent intentofbingli4toagain = new Intent(BingLi4.this,
						AllIllness.class);
				intentofbingli4toagain.putExtras(bundleofagainofBL4);
				startActivity(intentofbingli4toagain);
				finish();
			} catch (RuntimeException e) {
				android.app.AlertDialog.Builder builderofBL4oftoA = new AlertDialog.Builder(
						BingLi4.this);
				builderofBL4oftoA.setMessage("该功能不能使用,请重试或者使用其他功能");
				builderofBL4oftoA.create().show();
			}
		}

	}



	class listenetofbingli4toJBXQ implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			try {

				Bundle bundleofBL4 = new Bundle();
				bundleofBL4.putString("shuju", animalIDofBL4);
				Intent intentofbingli4toJBXQ = new Intent(BingLi4.this,
						JBXQ.class);
				intentofbingli4toJBXQ.putExtras(bundleofBL4);
				startActivity(intentofbingli4toJBXQ);
				finish();
			} catch (RuntimeException e) {
				android.app.AlertDialog.Builder builderofBL4oftoJBXQ = new AlertDialog.Builder(
						BingLi4.this);
				builderofBL4oftoJBXQ.setMessage("该功能不能使用,请重试或者使用其他功能");
				builderofBL4oftoJBXQ.create().show();
			}

		}

	}

	public Handler handleofBL4 = new Handler() {

		public void handleMessage(Message msg) {

			if (msg.what == 1) {
				textofBL4.setText("病名：" + nameofBL4 + " " + "概率：" + "0"
						+ GLofBL4);
			}
		}
	};

}
